package com.iflytek.exam.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.iflytek.exam.domain.TbQuestionSummary;
import com.iflytek.exam.dto.TbAllDTO;
import com.iflytek.exam.mapper.TbQuestionSummaryMapper;
import com.iflytek.exam.vo.CategoryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.iflytek.exam.mapper.TbAllMapper;
import com.iflytek.exam.domain.TbAll;
import com.iflytek.exam.service.ITbAllService;

/**
 * 试题管理Service业务层处理
 *
 * @author Dogdog
 * @date 2024-07-06
 */
@Service
public class TbAllServiceImpl implements ITbAllService {
    @Autowired
    private TbAllMapper tbAllMapper;


    @Autowired
    private TbQuestionSummaryMapper questionSummaryMapper;

    /**
     * 查询试题管理
     *
     * @param id 试题管理主键
     * @return 试题管理
     */
    @Override
    public TbAll selectTbAllById(Long id) {
        return tbAllMapper.selectTbAllById(id);
    }

    /**
     * 查询试题管理列表
     *
     * @param tbAll 试题管理
     * @return 试题管理
     */
    @Override
    public List<TbAll> selectTbAllList(TbAll tbAll) {
        return tbAllMapper.selectTbAllList(tbAll);
    }

    /**
     * 新增试题管理
     *
     * @param tbAll 试题管理
     * @return 结果
     */
    @Override
    public int insertTbAll(TbAll tbAll) {
        return tbAllMapper.insertTbAll(tbAll);
    }

    /**
     * 修改试题管理
     *
     * @param tbAll 试题管理
     * @return 结果
     */
    @Override
    public int updateTbAll(TbAll tbAll) {
        return tbAllMapper.updateTbAll(tbAll);
    }

    /**
     * 批量删除试题管理
     *
     * @param ids 需要删除的试题管理主键
     * @return 结果
     */
    @Override
    public int deleteTbAllByIds(Long[] ids) {
        return tbAllMapper.deleteTbAllByIds(ids);
    }

    /**
     * 删除试题管理信息
     *
     * @param id 试题管理主键
     * @return 结果
     */
    @Override
    public int deleteTbAllById(Long id) {
        return tbAllMapper.deleteTbAllById(id);
    }

    @Override
    public List<TbAllDTO> selectTbAllDTOList(TbAll tbAll) {
        List<TbAll> tbAllList = this.selectTbAllList(tbAll);
        List<TbAllDTO> tbAllDTOList = new ArrayList<>();
        tbAllList.forEach(tbAll1 -> {
            TbQuestionSummary tbQuestionSummary = questionSummaryMapper.selectTbQuestionSummaryById(tbAll1.getQuBankId());
            TbAllDTO tbAllDTO = new TbAllDTO();
            BeanUtils.copyProperties(tbAll1, tbAllDTO);
            tbAllDTO.setName(tbQuestionSummary.getName());
            tbAllDTOList.add(tbAllDTO);
        });
        return tbAllDTOList;
    }

    @Override
    public List<String> getCategoryFirst() {
        return tbAllMapper.getCategoryFirst();
    }

    @Override
    public List<String> getCategorySecond() {
        return tbAllMapper.getCategorySecond();
    }

    @Override
    public List<String> getCategoryThrid() {
        return tbAllMapper.getCategoryThrid();
    }

    @Override
    public IPage<TbAllDTO> selectTbAllDTOListByPage(TbAll tbAll, Integer pageNum, Integer pageSize) {
        return tbAllMapper.selectTbAllDTOListByPage(tbAll, pageNum, pageSize);
    }

    @Override
    public CategoryVo selectCategoryList() {
        //查询每种分类去重之后的值
        List<String> categoryFirstList = tbAllMapper.getCategoryFirst();
        List<String> categorySecondList = tbAllMapper.getCategorySecond();
        List<String> categoryThirdList = tbAllMapper.getCategoryThrid();
        CategoryVo categoryVo = new CategoryVo();
        categoryVo.setCategoryThird(categoryFirstList.size() == 0 ? null : categoryFirstList);
        categoryVo.setCategorySecond(categorySecondList.size() == 0 ? null : categorySecondList);
        categoryVo.setCategoryThird(categoryThirdList.size() == 0 ? null : categoryThirdList);
        return categoryVo;
    }
}
